package com.tool_stdy.utils;

import java.lang.reflect.Field;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.tool_stdy.annotation.SqlEntry;

public class AnnotationAnalyUtils<T> {

    /**
     * 通过泛型的方式获取 字段、值、注解
     * @param clazz
     * @param o
     * @throws IllegalArgumentException
     * @throws IllegalAccessException
     */
    public void AnalySqlEntryByT(T t,Class<?> clazz) throws IllegalArgumentException, IllegalAccessException {
        Field[] declaredFields = clazz.getDeclaredFields();
        for (Field f : declaredFields) {
            SqlEntry annotation = f.getAnnotation(SqlEntry.class);
            if (annotation != null) {
                f.setAccessible(true);
                System.out.println("字段:" + f.getName() + "--注解:" + annotation.name() + "--->值"+f.get(t));
            }
        }
    }
    
    public void AnalySqlEntryByJson(String json,Class<?> clazz) throws IllegalArgumentException, IllegalAccessException, NoSuchFieldException, SecurityException {
       JSONObject jo = JSON.parseObject(json);
       Object obj = JSON.parseObject(json, clazz);
       for(String key :jo.keySet()){
           Field field = clazz.getDeclaredField(key);
           SqlEntry annotation = field.getAnnotation(SqlEntry.class);
          if(annotation!=null){
//               写法1：从json中获取方法名和 value 
//               System.out.println("字段:"+key+"--注解:"+annotation.name()+"--->值"+jo.get(key));
              //写法2 从Field中获取字段名和内容
              field.setAccessible(true);
              System.out.println("字段:"+field.getName()+"--注解:"+annotation.name()+"--->值"+field.get(obj));
           }
       }
    }
}
